﻿Imports System.IO
Public Class IOHandler
    Public Shared Sub _construct(ByVal command As String, ByVal dir As String)
        Dim commandArray() As String = command.Split(" ")
        Select Case commandArray(0)
            Case Is = "ls"
                Call ls(command, dir)
        End Select
    End Sub

    Public Shared Sub ls(ByVal command As String, ByVal dir As String)
        If command = "ls" Then
            command = "ls -all"
        End If
        Dim pathlength As Integer = 1
        Dim commandArray() As String = command.Split(" ")
        If dir.Substring(Len(dir) - 2, 2) = ":\" Then
            pathlength = Len(dir)
        Else
            pathlength = Len(dir) + 1
        End If

        If UBound(commandArray) = 1 Then
            Dim File As String() = Directory.GetFiles(dir)
            Dim Dirs As String() = Directory.GetDirectories(dir)
            'DEBUG: MsgBox(UBound(File) & UBound(Dirs))
            Select Case commandArray(1)
                Case Is = "-files"
                    Dim Filename As String
                    Console.ForegroundColor = ConsoleColor.Gray
                    Console.WriteLine()
                    For Each Filename In File
                        Filename = Filename.Remove(0, pathLength)
                        Console.WriteLine(Filename)
                    Next
                    If UBound(File) = -1 Then
                        Console.ForegroundColor = ConsoleColor.DarkGray
                        Console.WriteLine("No files found.")
                        Console.ResetColor()

                    End If
                    Console.WriteLine()
                    Console.ResetColor()
                Case Is = "-dir"
                    Dim DirectoryName As String
                    Console.ForegroundColor = ConsoleColor.Cyan
                    Console.WriteLine()
                    For Each DirectoryName In Dirs
                        DirectoryName = DirectoryName.Remove(0, pathLength)
                        Console.WriteLine(DirectoryName)
                    Next
                    If UBound(Dirs) = -1 Then
                        Console.ForegroundColor = ConsoleColor.DarkGray
                        Console.WriteLine("No Directories found.")
                        Console.ResetColor()

                    End If
                    Console.WriteLine()
                    Console.ResetColor()
                Case Is = "-all"
                    Dim DirectoryName As String
                    Console.WriteLine()
                    Console.ForegroundColor = ConsoleColor.Cyan
                    For Each DirectoryName In Dirs
                        DirectoryName = DirectoryName.Remove(0, pathLength)
                        Console.WriteLine(DirectoryName)
                    Next
                    Console.ForegroundColor = ConsoleColor.Gray
                    Dim Filename As String
                    For Each Filename In File
                        Filename = Filename.Remove(0, pathLength)
                        Console.WriteLine(Filename)
                    Next
                    If UBound(Dirs) = -1 Then
                        If UBound(File) = -1 Then
                            Console.ForegroundColor = ConsoleColor.DarkGray
                            Console.WriteLine("Directory Empty.")
                            Console.ResetColor()
                        End If
                    End If
                    Console.WriteLine()
                    Console.ResetColor()

                Case Is = "-ext"
                    '-ext with no extension present
                    Console.ForegroundColor = ConsoleColor.DarkGray
                    Console.WriteLine("No file extension specified." & vbCrLf)
                    Console.ResetColor()
                    Console.WriteLine()

                Case Else
                    Console.ForegroundColor = ConsoleColor.DarkGray
                    Console.WriteLine("The switch """ & commandArray(1) & """ was not recognized." & vbCrLf)
                    Console.ResetColor()
                    Console.WriteLine()
            End Select

        ElseIf UBound(commandArray) = 2 Then
            Select Case commandArray(1)
                Case Is = "-ext"
                    Dim File_ext As String() = Directory.GetFiles(dir, "*." & commandArray(2))
                    Dim f_ext As String
                    Dim ext As String = String.Join(",", File_ext)
                    'DEBUG: MsgBox(ext & File_ext.Length)

                    If File_ext.Length = 0 Then
                        Console.ForegroundColor = ConsoleColor.DarkGray
                        Console.WriteLine("No files with that extensions are present.")
                    End If
                    Try
                        Console.ForegroundColor = ConsoleColor.Gray
                        For Each f_ext In File_ext
                            Console.WriteLine(f_ext.Remove(0, pathLength))
                        Next
                    Catch ex As Exception
                    End Try
                    Console.WriteLine()
                    Console.ResetColor()
            End Select

        ElseIf UBound(commandArray) > 2 Then
            Console.ForegroundColor = ConsoleColor.DarkGray
            Console.WriteLine("The command """ & commandArray(0) & """ does not accept this number of arguments." & vbCrLf)
            Console.ResetColor()
            Console.WriteLine()
        End If



    End Sub
End Class
